<?php
class Orz extends backend_Controller
{
    //上传文件夹(如果文件夹无权限或不存在 会提示abort-流产 错误提示！)
    var $upload_dir = 'assets/uploads/files';

    public function __construct()
    {
        parent::__construct();
        //注册菜单  == 亦可在 backend_Controller 中配置
        $this->menu = array("menu" => array(
        	array("title" => "宝贝相关", "url" => base_url("admin/orz/baby/")),
            array('title' => '广告焦点图','url' => base_url('admin/orz/adfocus/')),
            array('title' => '专辑','url' => base_url('admin/orz/columnv'))
        ));

        $this->upload_dir = './upload/baby/';
        $this->adfocus_dir = './upload/adfocus/';
        $this->column_dir = './upload/column/';
        $this->free_baby_dir = './upload/free_baby/';
        $this->life_news_dir = './upload/life_news/';
    }

    public function index()
    {
        $this->baby();
    }

    /**
     * 宝贝
     */
    public function baby()
    {
    	//baby_type中的id是宝贝的分类id
        $crud = new grocery_CRUD ();
        $crud->set_theme('datatables');
        $crud->set_table('baby');

        $crud->set_subject('宝贝');

        $crud->display_as('tb_url', '淘宝链接')
            ->display_as('small_img', '宝贝小图片(专辑用到60*60)')
            ->display_as('big_img','宝贝大图210*314')
            ->display_as('desc','宝贝简述')
            ->display_as('style_id','宝贝风格')
            ->display_as('baby_class_id','宝贝分类')
            ->display_as('baby_sonclass_id','宝贝子分类')
            ->display_as('band','品牌名')
            ->display_as('ex_price', '宝贝原价')
            ->display_as('now_price','宝贝现价')
            ->display_as('info','宝贝详细信息')
            ->display_as('updatetime','更新宝贝信息时间')
            ->display_as('addtime','添加时间');
        
        $crud->set_relation('style_id','common','name',array('sign'=>'cloth_style'));
        $crud->set_relation('baby_class_id','common','name',array('sign'=>'cloth_class'));
        $crud->set_relation_n_n('baby_sonclass_id','common','baby_type','id','cloth_class_id','name','id');


        /*
         $crud->unset_add();
        $crud->unset_delete();
        $crud->unset_columns('productDescription');
         
        if($crud->getState() == 'add' || $crud->getState() == 'edit'){
        $sign = 'news_info';
        }*/
           
        $thumbnail         = new Thumbnails_ ();
        $thumbnail->config = array(
				            "small_img" => array(
				                "w" => 60,
				                "h" => 60
				            ),
			        		"big_img" => array(
		        				"w" => 210,
		        				"h" => 314
			        		));
        
        $crud->callback_before_upload(array(
            $thumbnail,
            "thumbnail_callback"
        ));

        $crud->required_fields(array(
            'big_img',
        	'small_img',
            'tb_url',
        ));

        $crud->set_field_upload('big_img', $this->upload_dir);
        $crud->set_field_upload('small_img', $this->upload_dir);

        $crud->set_rules('ex_price','原价','numeric');
        $crud->set_rules('now_price','现价','numeric');
        
        $crud->columns('id','tb_url','small_img','desc','band','ex_price','now_price');

        $output = $crud->render();

        $this->backend_common_template($output);
    }
    
    /**
     * 生活私搭/新闻资讯
     */
    function life_news(){
    	$crud = new grocery_CRUD ();
    	$crud->set_theme('datatables');
    	$crud->set_table('life_news');
    	 
    	$crud->set_subject('生活私搭（life_share）/新闻资讯（news_info）');
    	 
    	$crud->display_as('title', '标题')
    	->display_as('content', '正文')
    	->display_as('sign','生活私搭?新闻资讯')
    	->display_as('isindex','是否在首页显示，1为显示，默认为0不显示。')
    	->display_as('ishot','是否是热闹话题，1为是，默认为0不是。')
    	->display_as('class_id','分类')
    	->display_as('member_id','作者')
    	->display_as('updatetime','更新信息时间')
    	->display_as('img','图片')
    	->display_as('addtime','添加时间');
/*     	
    	$thumbnail         = new Thumbnails_ ();
    	$thumbnail->config = array(
    			"img" => array(
    					"w" => 60,
    					"h" => 60
    			));
    	
    	$crud->callback_before_upload(array(
    			$thumbnail,
    			"thumbnail_callback"
    	)); */
    	
    	$crud->set_field_upload('img', $this->life_news_dir);
    	 
    	$crud->set_relation('member_id','member','name','authority >3');
    	$crud->set_relation('class_id','common','name',array('sign'=>'life_news'));
    	 
    	$crud->columns('id','title','isindex','class_id','member_id','addtime','updatetime','sign','ishot');

    	$crud->callback_add_field('sign',array($this,'add_field_callback'));
    	//$crud->callback_edit_field('sign',array($this,'add_field_callback'));
    	$output = $crud->render();
    	$this->backend_common_template($output);
    }
    
    function add_field_callback(){
    	return '<select style="width: 300px;" data-placeholder="生活私搭？新闻资讯" name="sign">
				<option value="life_share">生活私搭</option>
				<option value="news_info">新闻资讯</option>
				</select>';
    }


    /**
     * 免费申请试用品
     */
    public function free_baby()
    {
    	$crud = new grocery_CRUD ();
    	$crud->set_theme('datatables');
    	$crud->set_table('free_baby');
    
    	$crud->set_subject('免费申请试用品');
    
    	$crud->display_as('helper_url', '赞助商链接')
    	->display_as('helper_img', '赞助商logo')
    	->display_as('big_img','商品大图')
    	->display_as('desc','商品简述')
    	->display_as('band','品牌名')
    	->display_as('ex_price', '商品原价')
    	->display_as('info','商品详细信息')
    	->display_as('updatetime','商家更新商品信息时间')
    	->display_as('all_num', '商品提供总数')
    	->display_as('left_num','商品剩余数量')
    	->display_as('endtime','商品截止申请时间')
    	->display_as('property','商品规格/属性')
    	->display_as('isapply','是否为免费试用品，默认1为是。')
    	->display_as('addtime','添加时间');

    	$crud->set_field_upload('big_img', $this->free_baby_dir);
    	$crud->set_field_upload('helper_img', $this->free_baby_dir);
    
    	$crud->columns('id','band','all_num','left_num','desc','ex_price','property','endtime','addtime','isapply');
    
    	$output = $crud->render();
    	
    	/*
    	if($crud->getState() == 'add' || $crud->getState() == 'edit'){
    		$applyer_ip = ip2long($this->input->ip_address());
    	}else{
    		
    	}
    	*/
    	$this->backend_common_template($output);
    }
    
    

    /**
     * 广告焦点图
     */
    public function adfocus()
    {
        $crud = new grocery_CRUD ();
        $crud->set_theme('flexigrid');
        $crud->set_table('adfocus');

        $crud->set_subject('广告焦点图');

        $crud->display_as('url', '链接')
            ->display_as('img', '焦点图(专辑用到578*210)')
            ->display_as('desc','宝贝描述')
            ->display_as('addtime','添加时间')
            ->display_as('type','属性')
            ->display_as('id','ID');

        $thumbnail         = new Thumbnails_ ();
        $thumbnail->config = array(
            "img" => array(
                "w" => 578,
                "h" => 210
            ));
        $crud->callback_before_upload(array(
            $thumbnail,
            "thumbnail_callback"
        ));

        $crud->required_fields(array('img'));

        $crud->set_field_upload('img', $this->adfocus_dir);
        $crud->columns('id','url','img','desc','addtime','type');

        $output = $crud->render();

        $this->backend_common_template($output);
    }


    /**
     *专辑列表
     */
    public function columnv()
    {
        $crud = new grocery_CRUD ();
        $crud->set_theme('flexigrid');
        $crud->set_table('column');

        $crud->set_subject('专辑');

        $crud->display_as('classid', '专辑分类')
            ->display_as('img', '专辑图片210*210')
            ->display_as('desc','专辑描述')
            ->display_as('addtime','添加时间')
            ->display_as('title','专辑标题')
            ->display_as('memberid','创建的会员')
            ->display_as('updatetime','更新时间')
            ->display_as('id','ID');

        $thumbnail         = new Thumbnails_ ();
        $thumbnail->config = array(
            "img" => array(
                "w" => 210,
                "h" => 210
            ));
        $crud->callback_before_upload(array(
            $thumbnail,
            "thumbnail_callback"
        ));

        $crud->required_fields(array('img'));

        $crud->set_field_upload('img', $this->column_dir);
        $crud->columns('id','classid','img','desc','addtime','title','memberid','updatetime');

        $output = $crud->render();

        $this->backend_common_template($output);
    }


}